package com.xpgk.mvc.statistic.loseWeightList;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.bouncycastle.crypto.tls.MACAlgorithm;

import com.jfinal.aop.Before;
import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.xpgk.annotation.Service;
import com.xpgk.constant.ConstantRender;
import com.xpgk.dto.RenderBean;
import com.xpgk.mvc.base.BaseService;
import com.xpgk.mvc.bloseWeight.DelWeiTarget;
import com.xpgk.mvc.statistic.dailyNursing.DailyNursingStatisticSQL;
import com.xpgk.mvc.statistic.spropagandaTraining.SpropagandaTrainingSQL;
import com.xpgk.tools.ToolDateTime;


@Service(name = LoseWeightListService.serviceName)
public class LoseWeightListService extends BaseService{

	@SuppressWarnings("unused")
	private static final Log log = Log.getLog(LoseWeightListService.class);
	public static final String serviceName = "loseWeightListService";
	
	
	/*
	 * 顾客数据查询
	 */
	
	public List<Record> customerDataQuey(String organIdsEmployee,String arrivedMinDate,String arrivedMaxDate,String customerName) throws ParseException{
		String sql = LoseWeightListSQL.loseweightlist_splitPage;
		String sqlLose = getSqlMy(LoseWeightListSQL.loseweightlist_customerLoseWeight);
		String sqlLoseWeight = getSqlMy(DailyNursingStatisticSQL.sqlId_selDeltaget);
		Map<String, Object> queryParam = new HashMap<String, Object>();
	    queryParam.put("organIdsEmployee", organIdsEmployee);
	    queryParam.put("arrivedMinDate", arrivedMinDate);
	    queryParam.put("arrivedMaxDate", arrivedMaxDate);
	    queryParam.put("customerName", customerName);
		List<Record> result = find(sql,queryParam);
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-DD" );
		Date  startDate=simpleDateFormat.parse(arrivedMinDate);
		Date  endDate=simpleDateFormat.parse(arrivedMaxDate);
		//添加减重情况
		for(int i =0;i<result.size();i++){
			//当期减重斤数
			float loseweight=0;
			String customerIds = result.get(i).getStr("ids");
			List<Record> recordList = Db.find(sqlLose, customerIds,arrivedMinDate,arrivedMaxDate);
			//zheli---------------------------------------------------------------
			if(recordList.size()>0 && recordList!=null){
				float lostweight = recordList.get(0).getFloat("weight");
				float firstweight = recordList.get(recordList.size()-1).getFloat("weight");
				loseweight = lostweight-firstweight;
			}
			result.get(i).set("thisloseweight", (int)(loseweight+0.5));
			//当期减重目标
			float  totaldel=0;
			HashMap<String, Object> paramLoseWeight = new HashMap<String,Object>();
			paramLoseWeight.put("organIdsEmployee", customerIds);
			List<DelWeiTarget>  dtList=DelWeiTarget.dao.find(sqlLoseWeight,customerIds,arrivedMaxDate,arrivedMaxDate,arrivedMinDate,arrivedMinDate);
			if(dtList!=null && dtList.size()>0){
				//获取第一条的平均减重
				float  delaverage=dtList.get(0).getFloat(DelWeiTarget.column_delWeiTarget)/(ToolDateTime.getDateDaySpace(dtList.get(0).getDate(DelWeiTarget.column_startTime), dtList.get(0).getDate(DelWeiTarget.column_endTime))+1);
				//获取当月第一天到第一期减重应减的斤数
				float  startdel=(ToolDateTime.getDateDaySpace(startDate,  dtList.get(0).getDate(DelWeiTarget.column_endTime))+1)*Float.parseFloat(String.format("%.2f", delaverage));
				//获取最后一条平均减重
				float  lastaverage=dtList.get(dtList.size()-1).getFloat(DelWeiTarget.column_delWeiTarget)/(ToolDateTime.getDateDaySpace(dtList.get(dtList.size()-1).getDate(DelWeiTarget.column_startTime), dtList.get(dtList.size()-1).getDate(DelWeiTarget.column_endTime))+1);
				//获取当月最后一天到最后一期减重应减的斤数
				float  enddel=ToolDateTime.getDateDaySpace(dtList.get(dtList.size()-1).getDate(DelWeiTarget.column_startTime),endDate)*Float.parseFloat(String.format("%.2f", lastaverage));
				//总减重目标
				totaldel=startdel+enddel;
				for(int n=1;n<dtList.size()-1;n++){
					totaldel+=dtList.get(n).getFloat(DelWeiTarget.column_delWeiTarget);
				}
				result.get(i).set("thisMonthAim", (int)(totaldel+0.5));
			}
			//累计实减斤数
			float sumLoseWeight=0;
			for(int j=0;j<recordList.size();j++){
				float lastWeight = recordList.get(j).getFloat("lastWeight");
				float weight = recordList.get(j).getFloat("weight");
				if(lastWeight>=weight){
					sumLoseWeight+=lastWeight-weight;
				}
			}
			result.get(i).set("sumLoseWeight", sumLoseWeight);
			//未减斤数
			int nolose = (int)(totaldel-loseweight+0.5);
			if(nolose<0){
				result.get(i).set("nolose","已达标");
			}else{
				result.get(i).set("nolose", nolose);
			}
			//总减重率
			int round = Math.round(loseweight/totaldel*100);
			if(totaldel>0){
				result.get(i).set("sumLoseWeightResult", round+"%");
			}else{
				result.get(i).set("sumLoseWeightResult", "本期无减重目标");
			}
			//是否达到标体
		
			if(result.get(i).getFloat("nowWeight")>result.get(i).getFloat("targetWeight")){
				result.get(i).set("isdose", "未达标");
			}else{
				result.get(i).set("isdose", "达标");
			}
		}
		return result;
	}
}
